//清理个人空间的表单
function personSpaceFormClean(type) {
    if (type == 0)//如果是清理个人基本信息板块的话
        personalMessageForm.reset();
    else if (type == 1)//如果是清理密码板块的话
        updatePasswordForm.reset();
}

//获取个人基本信息
function getPersonBasicData() {
    var result;
    Ext.Ajax.request({//获取当前节点的信息
        url: zuulUrl + '/' + authServiceName + '/user/basic/' + currentUserId,
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            result = obj;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

//填充个人基本信息表单
function personalMessageFormPutDataInto(data) {
    personalMessageForm.down('#userName').setValue(data.basic.username);
    personalMessageForm.down('#realName').setValue(data.basic.realName);
    var roles = '[ ';
    for (var i = 0, length = data.roles.length; i < length; i++) {
        if (data.roles[i] != '基础角色')
            roles = roles + data.roles[i] + ' ';
    }
    roles = roles + ']';
    personalMessageForm.down('#roles').setValue(roles);
}

//当点击某一个tab之后要做的事情
function personSpaceTabClick(type) {
    if (type == 0) {
        personSpaceFormClean(0);
        var data = getPersonBasicData();
        personalMessageFormPutDataInto(data);
    } else if (type == 1)
        personSpaceFormClean(1);
    else if (type == 2) {
        personalSpaceTreeStore.reload();
        personalViewPort.down('#centerRegion').removeAll(false);
    }
}

//修改个人基本信息
function personalMessageUpdate() {
    var result = new Object();
    if (personalMessageForm.isValid()) {
        result['id'] = currentUserId;
        result['username'] = personalMessageForm.down('#userName').getValue();
        result['realName'] = personalMessageForm.down('#realName').getValue();
        Ext.Ajax.request({
            url: zuulUrl + '/' + authServiceName + '/user/basic',
            method: 'put',
            async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            defaultXdrContentType: 'application/json',
            jsonData: JSON.stringify(result),
            success: function (response, opts) {
                //成功了的话要把储存的用户昵称和右上角的用户昵称给修改掉
                Ext.Msg.alert("成功", "保存成功");
                currentUser = result['realName'];
                updateUserTripButtonText(currentUser);
            },
            failure: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                    Ext.Msg.alert("错误", obj.message);
                else
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    } else
        Ext.Msg.alert('错误', '测试计划信息请填写完全！');
}

//修改个人密码
function personalPasswordUpdate() {
    var result = new Object();
    if (updatePasswordForm.isValid()) {
        var oldPassword = updatePasswordForm.down('#oldPassword').getValue();
        var newPassword1 = updatePasswordForm.down('#newPassword1').getValue();
        var newPassword2 = updatePasswordForm.down('#newPassword2').getValue();
        if (newPassword1 != newPassword2)
            Ext.Msg.alert('错误', '新密码两次输入不一致！');
        else {
            Ext.Ajax.request({
                url: zuulUrl + '/' + authServiceName + '/user/pwd',
                method: 'put',
                params: {
                    userId: currentUserId,
                    oldPassword: oldPassword,
                    newPassword: newPassword2
                },
                async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                success: function (response, opts) {
                    Ext.Msg.alert("成功", "修改成功");
                },
                failure: function (response, opts) {
                    var obj = Ext.decode(response.responseText);
                    if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                        Ext.Msg.alert("错误", obj.message);
                    else
                        Ext.Msg.alert("错误", "该功能暂时不可用！");
                }
            });
        }
    } else
        Ext.Msg.alert('错误', '测试计划信息请填写完全！');
}

//获取节点的孩子节点
function personSpaceLeftTreeGetChildren(record, panel) {
    if (record.get('leaf') == false && record.get('isHas') == 0) {
        var id = record.get('id');
        var depth = record.getDepth();
        var url;
        if (depth == 1)
            url = panel.depth2ChildrenUrl + record.get('id') + '/' + currentUserId;
        else if (depth == 2)
            url = panel.depth3ChildrenUrl + record.get('id');
        Ext.Ajax.request({
            url: url,
            method: 'get',
            async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            success: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                Ext.each(obj, function (node) {// 获得数据之后先将title的值赋值给text
                    node.text = node['title'];
                    if (depth==2||node.childrenCount == 0)//如果这个节点的可显示节点数量等于0则将此文件夹设置为不可展开
                        node.expandable = false;
                });
                record.appendChild(obj);
                if (record.data.leaf == false && record.data.expanded == false)
                    record.set('expanded', true);
                record.set("isHas", 1);
            },
            failure: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                    Ext.Msg.alert("错误", obj.message);
                else
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    }
}

//点击树形菜单节点之后的操作
function personalSpaceTreeItemClick(record, panel) {
    personalViewPort.down('#centerRegion').removeAll(false);
    var depth = record.getDepth();
    personalSpaceCenterFormClean(record);
    var data = personalSpaceTreeItemGetData(depth, record, panel);
    personalSpaceTreeItemPutDataInto(data, record);
    personalSpaceShowFormToCenter(depth);
}

//将中心界面的表单清空
function personalSpaceCenterFormClean(record) {
    var depth = record.getDepth();
    if (depth == 1) {//如果是去清空班级信息的表单的话
        personalSpaceClassesDetailForm.reset();
        personalSpaceClassesMemberGridStore.removeAll();
        personalSpaceClassesTabPanel.setActiveTab(0);
    } else if (depth == 2) {//如果是去清空组信息的表单的话
        personalSpaceGroupDetailForm.reset();
        personalSpaceGroupMemberGridStore.removeAll();
        personalSpaceGroupTabPanel.setActiveTab(0);
    } else if (depth == 3) {//如果是去清空项目信息的表单的话
        personalSpaceProjectDetailForm.reset();
        personalSpaceGradeStore.removeAll();
        personalSpaceProjectTabPanel.setActiveTab(0);
    }
}

//去获取对应节点的详细信息
function personalSpaceTreeItemGetData(depth, record, panel) {
    var url;
    if (depth == 1)
        url = panel.depth2ClickUrl + record.data.id;
    else if (depth == 2)
        url = panel.depth3ClickUrl + record.data.id;
    else if (depth == 3)
        url = panel.depth4ClickUrl + record.data.id;
    var result;
    Ext.Ajax.request({
        url: url,
        method: 'get',
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            result = obj;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

//将对应树节点获取的数据填充到中心表单上
function personalSpaceTreeItemPutDataInto(data, record) {
    var depth = record.getDepth();
    if (depth == 1) {//如果是第一层的话,就是将班级信息覆盖写到表上
        personalSpaceClassesDetailForm.down('#name').setValue(data.name);
        personalSpaceClassesDetailForm.down('#creater').setValue(data.createrName);
        personalSpaceClassesDetailForm.down('#leader').setValue(data.leaderName);
        personalSpaceClassesDetailForm.down('#stage').setValue('第' + data.stage + '期');
        personalSpaceClassesDetailForm.down('#createDate').setValue(data.createDate);
        personalSpaceClassesDetailForm.down('#describe').setValue(data.describe);
        personalSpaceClassesMemberGridStore.setData(data.members);
    } else if (depth == 2) {//如果是第二层的话,就是将组信息覆盖写到表上
        personalSpaceGroupDetailForm.down('#name').setValue(data.name);
        personalSpaceGroupDetailForm.down('#describe').setValue(data.describe);
        personalSpaceGroupDetailForm.down('#createDate').setValue(data.createDate);
        personalSpaceGroupMemberGridStore.setData(data.members);
    } else if (depth == 3) {//如果是第三层的话,就是项目信息覆盖写到表上
        personalSpaceProjectDetailForm.down('#name').setValue(data.name);
        personalSpaceProjectDetailForm.down('#system').setValue(data.systemName);
        personalSpaceProjectDetailForm.down('#leadingCadre').setValue(data.leadingCadreName);
        personalSpaceProjectDetailForm.down('#describe').setValue(data.describe);
        personalSpaceProjectDetailForm.down('#createDate').setValue(data.createDate);
    }
}

//将对应的中心表单显示到中心界面
function personalSpaceShowFormToCenter(depth) {
    if (depth == 1)
        personalViewPort.down('#centerRegion').add(personalSpaceClassesTabPanel);
    else if (depth == 2)
        personalViewPort.down('#centerRegion').add(personalSpaceGroupTabPanel);
    else if (depth == 3)
        personalViewPort.down('#centerRegion').add(personalSpaceProjectTabPanel);
}